package redis

import (
	"github.com/fantomo/go-iching/config"
	"github.com/go-redis/redis"
	"time"
)

var RdConf = config.GetConf().Redis

var rd *redis.Client = redis.NewClient(&redis.Options{
	Addr:     RdConf.Addr,
	Password: RdConf.Password,
	DB:       RdConf.DB,
})

// SetKey 设置key
func SetKey(key string, value interface{}, expiration int) error {
	err := rd.Set(key, value, time.Duration(expiration)*time.Second).Err()
	if err != nil {
		return err
	}
	return nil
}

// DelKey 删除key
func DelKey(key string) {
	rd.Del(key)
}

// GetValue 通过key获取value
func GetValue(key string) (any, error) {
	res, err := rd.Get(key).Result()
	if err != nil {
		return nil, err
	}
	return res, nil
}
